﻿using AutoMapper;
using Common;
using Common.Dtos;
using Common.LayPage;
using Sigma.Admin.API.Context.Entity;
using Sigma.Admin.API.Context.UnitOfWork;

namespace Sigma.Admin.API.Services
{
    public class PermissionService : IPermissionService
    {
        private readonly IUnitOfWork _work;
        private readonly IMapper _mapper;


        public PermissionService(IUnitOfWork work, IMapper mapper)
        {
            _work = work;
            _mapper = mapper;
        }
        public Task<ApiResponse> AddAsync(PermissionDto model)
        {
            try
            {
                var per = _mapper.Map<PermissionEntity>(model);
                var _repository = _work.GetRepository<PermissionEntity>();
                var exist = _repository.Exists(x => (x.PermissionKey == model.PermissionKey || x.TitleName == model.TitleName));
                if (exist)
                {
                    return Task.FromResult(new ApiResponse("已存在许可证名字、key！"));
                }
                per.CreateTime = DateTime.Now;
                per.UpdateTime = DateTime.Now;
                _repository.Insert(per);
                var res = _work.SaveChangesAsync();
                if(res.Result>0)
                    return Task.FromResult(new ApiResponse("添加成功", true));
                return Task.FromResult(new ApiResponse("添加失败"));
            }
            catch (Exception ex)
            {
                return Task.FromResult(new ApiResponse("操作失败：" + ex.Message));
            }
        }

        public Task<ApiResponse<List<PermissionDto>>> GetAllAsync()
        {
            try
            {
                var per_repo = _work.GetRepository<PermissionEntity>();
                var menu_repo = _work.GetRepository<MenuEntity>();
                var users = per_repo.GetAll().ToList();
                var menus = menu_repo.GetAll();
                if (users.Count == 0 || users is null)
                    return Task.FromResult(new ApiResponse<List<PermissionDto>>("无数据"));
                var dtos = new List<PermissionDto>();
                users.ForEach(x =>
                {
                    var dto = _mapper.Map<PermissionDto>(x);
                    if(dto is not null)
                    {
                        dto.IsPermission = true;
                        dto.IsView = false;
                        dto.ParentName = menus.Where(m => m.Id == x.PId).FirstOrDefault().TitleName;
                        dtos.Add(dto);
                    }
                        
                });
                
                return Task.FromResult(new ApiResponse<List<PermissionDto>>(null, true, dtos));
            }
            catch(Exception ex)
            {
                return Task.FromResult(new ApiResponse<List<PermissionDto>>("操作失败："+ex.Message));
            }
            
        }

        public Task<ApiResponse> DeleteAsync(PermissionDto dto)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }

        public Task<ApiResponse> GetAllAsync(QueryParameter query)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }

        public Task<ApiResponse> GetSingleAsync(int id)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }

        public Task<ApiResponse> UpdateAsync(List<PermissionDto> model)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }
        public Task<ApiResponse> AddAsync(List<PermissionDto> models)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }

        public Task<ApiResponse> DeleteAsync(List<PermissionDto> model)
        {
            throw new NotImplementedException();
        }
    }
}
